package com.glenet.salary.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.glenet.salary.entities.HrCompany;
import com.glenet.salary.util.SessionFactoryUtil;

public class HrCompanyDao {

	public boolean save(HrCompany company){
		boolean flag = false;
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			flag = session.insert("HrCompany.save", company) > 0 ? true : false;
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	@SuppressWarnings("unchecked")
	public List<HrCompany> getCompany(int offset, int limit){
		List<HrCompany> list = new ArrayList<HrCompany>(0);
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			list = session.selectList("HrCompany.index", null, new RowBounds(offset, limit));
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public HrCompany getCompany(Long id){
		HrCompany company = new HrCompany();
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			company = (HrCompany)session.selectOne("HrCompany.get", id);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return company;
	}
	
	public boolean delete(Long id){
		boolean flag = false;
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			flag = session.delete("HrCompany.delete", id) > 0 ? true : false;
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean update(HrCompany company){
		boolean flag = false;
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			flag = session.update("HrCompany.update", company) > 0 ? true : false;
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
}
